import { DbService } from '@app/db';
import { Injectable } from '@nestjs/common';
import * as dayjs from 'dayjs';
import { MoreThan } from 'typeorm';

@Injectable()
export class PlatformService {
    constructor(
    private db:DbService
    ){}

    async statistics() {
        const currentDate = new Date();  
        const todayTime = Math.floor(currentDate.setHours(0, 0, 0, 0) / 1000);
        const yesterdayTime = dayjs.unix(todayTime).subtract(1, 'day').unix()
        const total = await this.db.platform.findOne({id : MoreThan(0)})
        const today = await this.db.platformDailyIncome.findOne({date : todayTime}) //今日的收入
        const yesterday = await this.db.platformDailyIncome.findOne({date : yesterdayTime}) //昨日的收入

        const onlineDeviceCount = await this.db.device.count({connectStatus : 1})
        const offlineDeviceCount = await this.db.device.count({connectStatus : 0})

        return {
            onlineDeviceCount,
            offlineDeviceCount,
            total,
            today,
            yesterday
        }

    }
}
